package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.DealerApiService;
import com.molichuxing.gateway.bff.dealer.entity.request.create.DealerCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.create.DealerGoodsCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.create.FenceCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.create.StoreCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.*;
import com.molichuxing.gateway.bff.dealer.entity.response.*;
import com.molichuxing.gateway.bff.dealer.services.DealerBffService;
import com.molichuxing.gateway.bff.dealer.services.DealerGoodsBffService;
import com.molichuxing.gateway.bff.dealer.services.FenceBffService;
import com.molichuxing.gateway.bff.dealer.services.StoreBffService;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsVo;
import com.molichuxing.gateway.property.DealerStatusEnum;
import com.molichuxing.gateway.property.StoreStatusAndVerifyEnum;
import com.molichuxing.services.property.FenceStoreStockTypeEnum;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * 经销商相关接口
 *
 * @author zhaopenghui
 * @date 2019年8月26日
 */
@Api(tags = "经销商相关接口")
@RestController
@RequestMapping(value = "/dealer")
public class DealerApiServiceImpl extends RootApiService implements DealerApiService {
    private static final Logger logger = LoggerFactory.getLogger(DealerApiServiceImpl.class);

    @Resource
    DealerBffService dealerBffService;
    @Resource
    StoreBffService storeBffService;

    @Resource
    DealerGoodsBffService dealerGoodsBffService;
    @Resource
    FenceBffService fenceBffService;

    @Override
    @ApiOperation(value = "获取经销商列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "营业状态（0：全部，1：营业中，2：暂停营业，3：终止合作）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "shortName", value = "经销商名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "regionId", value = "城市ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "startCreateTime", value = "创建开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endCreateTime", value = "创建结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/dealer/list")
    public ResponseResult<Paged<DealerListVo>> getDealerPage(@RequestParam Map<String, Object> params,
                                                             @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<DealerListVo>> result = new ResponseResult<>();
        try {
            Integer status = ObjectCastUtil.castInteger(params.get("status"));
            if (status != null && DealerStatusEnum.ALL.getValue() == status) {
                params.put("status", null);
            }

            Object startCreateTime = params.get("startCreateTime");
            Object endCreateTime = params.get("endCreateTime");
            if (startCreateTime != null) {
                LocalDateTime dayStart = ObjectCastUtil.getDayStart(startCreateTime);
                params.put("startCreateTime", dayStart);
            }
            if (endCreateTime != null) {
                LocalDateTime dayEnd = ObjectCastUtil.getDayEnd(endCreateTime);
                params.put("endCreateTime", dayEnd);
            }

            if (null == pageNum) {
                pageNum = 1;
            }
            if (null == pageSize) {
                pageSize = 10;
            }
            return result.success(dealerBffService.getDealerPage(params, pageNum, pageSize));
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerPage]获取经销商列表信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerPage]获取经销商列表信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerPage]获取经销商列表信息异常", e);
            result.error("获取经销商列表信息异常");
        }
        return result;
    }

    /**
     * 获取经销商基础信息
     *
     * @param dealerId
     * @return
     */
    @Override
    @ApiOperation(value = "获取经销商基础信息")
    @ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer")
    @GetMapping(value = "/dealer/detail")
    public ResponseResult<DealerInfoVo> getDealerInfo(@RequestParam Integer dealerId) {
        ResponseResult<DealerInfoVo> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerInfo(dealerId));
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerInfo]获取经销商基础信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerInfo]获取经销商基础信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerInfo]获取经销商基础信息异常", e);
            result.error("获取经销商基础信息异常");
        }
        return result;
    }

    /**
     * 获取经销商电子签章信息
     *
     * @param dealerId
     * @return
     */
    @Override
    @ApiOperation(value = "获取经销商电子签章信息")
    @ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer")
    @GetMapping(value = "/dealer/electronic/detail")
    public ResponseResult<DealerEsignInfoVo> getDealerEsignInfo(@RequestParam Integer dealerId) {
        ResponseResult<DealerEsignInfoVo> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerEsignInfoVo(dealerId));
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerEsignInfo]获取经销商电子签章信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerEsignInfo]获取经销商电子签章信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerEsignInfo]获取经销商电子签章信息异常", e);
            result.error("获取经销商电子签章信息异常");
        }
        return result;
    }


    /**
     * 编辑经销商信息
     *
     * @param dealerModifyVo
     * @return
     */
    @Override
    @ApiOperation(value = "编辑经销商信息")
    @PostMapping(value = "/dealer/modify")
    @OptionalLogConfig(methods = "编辑经销商信息")
    public ResponseResult<Boolean> modifyDealer(@RequestBody DealerModifyVo dealerModifyVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.modifyDealer(dealerModifyVo));
        } catch (BizException e) {
            logger.error("[modifyDealer]编辑经销商信息异常 ", e);
            result.error(e.getMessage());
        } catch (IllegalArgumentException e) {
            logger.error("[modifyDealer]编辑经销商信息异常 ", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[modifyDealer]编辑经销商信息异常 ", e);
            result.error("编辑经销商信息异常");
        }
        return result;
    }

    /**
     * 编辑经销商营业状态
     *
     * @param dealerModifyStatusVo
     * @return
     */
    @Override
    @ApiOperation(value = "编辑经销商营业状态")
    @PostMapping(value = "/dealer/modifyStatus")
    @OptionalLogConfig(methods = "编辑经销商营业状态")
    public ResponseResult<Boolean> modifyDealerStatus(@RequestBody DealerModifyStatusVo dealerModifyStatusVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.modifyDealerStatus(dealerModifyStatusVo));
        } catch (IllegalArgumentException e) {
            logger.error("[modifyDealerStatus]编辑经销商营业状态异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[modifyDealerStatus]编辑经销商营业状态异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[modifyDealerStatus]编辑经销商营业状态异常 ", e);
            result.error("编辑经销商营业状态异常");
        }
        return result;
    }

    /**
     * 添加经销商
     *
     * @param dealerCreateVo
     * @return
     */
    @Override
    @ApiOperation(value = "添加经销商")
    @PostMapping(value = "/dealer/create")
    @OptionalLogConfig(methods = "添加经销商")
    public ResponseResult<Boolean> createDealer(@RequestBody DealerCreateVo dealerCreateVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.createDealer(dealerCreateVo));
        } catch (BizException e) {
            logger.error("[createDealer]添加经销商异常 ", e);
            result.error(e.getMessage());
        } catch (IllegalArgumentException e) {
            logger.error("[createDealer]添加经销商异常 ", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[createDealer]添加经销商异常 ", e);
            result.error("添加经销商异常");
        }
        return result;
    }


    /**
     * 经销商门店联动
     *
     * @return
     */
    @Override
    @ApiOperation(value = "经销商门店联动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "经销商营业状态（null：所有经销商，1：营业中）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "经销商类型(null：所有，1:直销，2：分销)", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "dealerId", value = "经销商ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "dealerIsShow", value = "没有门店，经销商是否显示（true:显示，false:不显示）", required = false, dataType = "Boolean")
    })
    @GetMapping(value = "/dealer/dealer/store/list")
    public ResponseResult<List<DealerStoreVo>> getDealerStore(Integer status, Integer type, Integer dealerId, Boolean dealerIsShow) {
        ResponseResult<List<DealerStoreVo>> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerStore(status, type, dealerId, dealerIsShow));
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerStore]获取经销商门店联动异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerStore]获取经销商门店联动异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerStore]获取经销商门店联动异常 ", e);
            result.error("获取经销商门店联动异常");
        }
        return result;
    }

    /**
     * 经销商列表
     *
     * @return
     */
    @Override
    @ApiOperation(value = "经销商列表")
    @GetMapping(value = "/dealer/all/list")
    public ResponseResult<List<DealerVo>> getDealerList() {
        ResponseResult<List<DealerVo>> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerList());
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerList]获取经销商信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerList]获取经销商信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerList]获取经销商信息异常 ", e);
            result.error("获取经销商信息异常");
        }
        return result;
    }

    /**
     * 经销商模糊查询
     *
     * @param dealerName
     * @param status
     * @param type
     * @return
     */

    @Override
    @ApiOperation(value = "经销商模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dealerName", value = "经销商名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "经销商营业状态（1：营业中，2：暂停营业）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "经销商类型(1:直销，2：分销)", required = false, dataType = "Integer"),
    })
    @GetMapping(value = "/dealer/fuzzy/list")
    public ResponseResult<List<DealerVo>> getDealerByShortNameFuzzy(String dealerName, Integer status, Integer type) {
        ResponseResult<List<DealerVo>> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerByShortNameFuzzy(dealerName, status, type));
        } catch (BizException e) {
            logger.error("[getDealerByShortNameFuzzy]获取经销商模糊查询异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerByShortNameFuzzy]获取经销商模糊查询异常 ", e);
            result.error("获取经销商模糊查询异常");
        }
        return result;
    }

    /**
     * 获取门店列表信息
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @ApiOperation(value = "获取门店列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "状态（0:全部，1：待审核，2：驳回审核，3：营业中，4:暂停营业）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "shortName", value = "门店名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "regionId", value = "城市ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "startApplyTime", value = "申请开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endApplyTime", value = "申请结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/dealer/store/list")
    public ResponseResult<Paged<StoreVo>> getStorePage(@RequestParam Map<String, Object> params, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<StoreVo>> result = new ResponseResult<>();
        try {
            Object startApplyTime = params.get("startApplyTime");
            Object endApplyTime = params.get("endApplyTime");
            if (startApplyTime != null) {
                LocalDateTime dayStart = ObjectCastUtil.getDayStart(startApplyTime);
                params.put("startApplyTime", dayStart);
            }
            if (endApplyTime != null) {
                LocalDateTime dayEnd = ObjectCastUtil.getDayEnd(endApplyTime);
                params.put("endApplyTime", dayEnd);
            }

            if (null == pageNum) {
                pageNum = 1;
            }
            if (null == pageSize) {
                pageSize = 10;
            }

            Integer type = ObjectCastUtil.castInteger(params.get("type"));
            if (type != null && StoreStatusAndVerifyEnum.ALL.getValue() == type) {
                params.put("type", null);
            } else if (type != null && StoreStatusAndVerifyEnum.UN_VERIFY.getValue() == type) {
                params.put("verifyStatus", StoreVerifyStatusEnum.UN_VERIFY.getValue());
            } else if (type != null && StoreStatusAndVerifyEnum.VERIFY_REJECT.getValue() == type) {
                params.put("verifyStatus", StoreVerifyStatusEnum.VERIFY_REJECT.getValue());
            } else if (type != null && StoreStatusAndVerifyEnum.OPEN.getValue() == type) {
                params.put("status", StoreStatusEnum.OPEN.getValue());
                params.put("verifyStatus", StoreVerifyStatusEnum.VERIFY_PASS.getValue());
            } else if (type != null && StoreStatusAndVerifyEnum.SUSPENSION.getValue() == type) {
                params.put("status", StoreStatusEnum.SUSPENSION.getValue());
                params.put("verifyStatus", StoreVerifyStatusEnum.VERIFY_PASS.getValue());
            } else {
                params.put("verifyStatus", type);
            }

            return result.success(storeBffService.getStorePage(params, pageNum, pageSize));
        } catch (IllegalArgumentException e) {
            logger.error("[getStorePage]获取门店列表信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getStorePage]获取门店列表信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getStorePage]获取门店列表信息异常", e);
            result.error("获取门店列表信息异常");
        }
        return result;
    }

    /**
     * 编辑门店信息
     *
     * @param storeModifyVo
     * @return
     */
    @Override
    @ApiOperation(value = "编辑门店信息")
    @PostMapping(value = "/dealer/store/modify")
    @OptionalLogConfig(methods = "编辑门店信息")
    public ResponseResult<Boolean> modifyStore(@RequestBody StoreModifyVo storeModifyVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            String businessStartTime = storeModifyVo.getBusinessStartTime();
            String busineesEndTime = storeModifyVo.getBusineesEndTime();
            if (StringUtils.isNoneBlank(businessStartTime) && StringUtils.isNotBlank(busineesEndTime)) {
                LocalTime startTime = DateUtil.parseTime(businessStartTime);
                LocalTime endTime = DateUtil.parseTime(busineesEndTime);
                boolean afterBoolean = endTime.isAfter(startTime);
                if (!afterBoolean) {
                    return result.error("营业时间不正确");
                }
            }

            return result.success(storeBffService.modifyStore(storeModifyVo));
        } catch (IllegalArgumentException e) {
            logger.error("[modifyStore]编辑门店信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[modifyStore]编辑门店信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[modifyStore]编辑门店信息异常 ", e);
            result.error("编辑门店信息异常");
        }
        return result;
    }

    /**
     * 编辑门店状态
     *
     * @param storeModifyStatusVo
     * @return
     */
    @Override
    @ApiOperation(value = "编辑门店营业状态")
    @PostMapping(value = "/dealer/store/modifyStatus")
    @OptionalLogConfig(methods = "编辑门店营业状态")
    public ResponseResult<Boolean> modifyStoreStatus(@RequestBody StoreModifyStatusVo storeModifyStatusVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(storeBffService.modifyStoreStatus(storeModifyStatusVo));
        } catch (IllegalArgumentException e) {
            logger.error("[modifyStoreStatus]编辑门店营业状态异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[modifyStoreStatus]编辑门店营业状态异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[modifyStoreStatus]编辑门店营业状态异常 ", e);
            result.error("编辑门店营业状态异常");
        }
        return result;
    }


    /**
     * 添加门店信息
     *
     * @param storeCreateVo
     * @return
     */
    @Override
    @ApiOperation(value = "添加门店信息")
    @PostMapping(value = "/dealer/store/create")
    @OptionalLogConfig(methods = "添加门店信息")
    public ResponseResult<Boolean> createStore(@RequestBody @Valid StoreCreateVo storeCreateVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            String businessStartTime = storeCreateVo.getBusinessStartTime();
            String busineesEndTime = storeCreateVo.getBusineesEndTime();
            if (StringUtils.isNoneBlank(businessStartTime) && StringUtils.isNotBlank(busineesEndTime)) {
                LocalTime startTime = DateUtil.parseTime(businessStartTime);
                LocalTime endTime = DateUtil.parseTime(busineesEndTime);
                boolean afterBoolean = endTime.isAfter(startTime);
                if (!afterBoolean) {
                    return result.error("营业时间不正确");
                }
            }

            return result.success(storeBffService.createStore(storeCreateVo));
        } catch (IllegalArgumentException e) {
            logger.error("[createStore]添加门店信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[createStore]添加门店信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[createStore]添加门店信息异常 ", e);
            result.error("添加门店信息异常");
        }
        return result;
    }

    /**
     * 门店审核
     *
     * @param storeModifyVerifyStatusVo
     * @return
     */
    @Override
    @ApiOperation(value = "门店审核")
    @PostMapping(value = "/dealer/store/check/create")
    @OptionalLogConfig(methods = "门店审核")
    public ResponseResult<Boolean> modifyStoreVerifyStatus(@RequestBody StoreModifyVerifyStatusVo storeModifyVerifyStatusVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            Integer storeId = storeModifyVerifyStatusVo.getStoreId();
            Integer verifyStatus = storeModifyVerifyStatusVo.getVerifyStatus();
            String rejectReason = storeModifyVerifyStatusVo.getRejectReason();
            String remark = storeModifyVerifyStatusVo.getRemark();

            return result.success(storeBffService.modifyStoreVerifyStatus(storeId, verifyStatus, rejectReason, remark));
        } catch (IllegalArgumentException e) {
            logger.error("[modifyStoreVerifyStatus]门店审核异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[modifyStoreVerifyStatus]门店审核异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[modifyStoreVerifyStatus]门店审核异常 ", e);
            result.error("门店审核异常");
        }
        return result;
    }

    /**
     * 门店详情
     *
     * @param storeId
     * @return
     */
    @Override
    @ApiOperation(value = "门店详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/dealer/store/datail")
    public ResponseResult<StoreInfoVo> getStoreInfo(@RequestParam Integer storeId) {
        ResponseResult<StoreInfoVo> result = new ResponseResult<>();
        try {
            return result.success(storeBffService.getStoreInfo(storeId));
        } catch (IllegalArgumentException e) {
            logger.error("[getStoreInfo]获取门店详情异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getStoreInfo]获取门店详情异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreInfo]获取门店详情异常", e);
            result.error("获取门店详情异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "商品分销列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shortName", value = "门店名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "regionId", value = "城市ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/dealer/goods/page")
    public ResponseResult<Paged<DealerGoodsVo>> getDealerGoodsPage(@RequestParam(required = false) Map<String, Object> params, @RequestParam Integer pageNum, @RequestParam Integer pageSize) throws Exception {
        ResponseResult<Paged<DealerGoodsVo>> result = new ResponseResult<>();
        try {
            params.put("status", StoreStatusEnum.OPEN.getValue());
            return result.success(dealerGoodsBffService.getDealerGoodsPage(params, pageNum, pageSize));
        } catch (Exception e) {
            logger.error("[getDealerGoodsPage]商品分销列表异常 ", e);
            result.error("商品分销列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "经销商已分销未分销商品分销列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "vehicleId", value = "车型ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "goodsType", value = "商品类型（1：专属，2：订阅）", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/dealer/goods/list")
    public ResponseResult<List<GoodsVo>> getDealerGoodsList(Integer dealerId, Integer goodsType, @RequestParam(required = false) Integer vehicleId) {
        ResponseResult<List<GoodsVo>> result = new ResponseResult<>();
        try {
            return result.success(dealerGoodsBffService.getDealerGoodsList(dealerId, goodsType, vehicleId));
        } catch (Exception e) {
            logger.error("[getDealerGoodsPage]商品分销列表异常 ", e);
            result.error("商品分销列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "分销商品")
    @PostMapping(value = "/dealer/goods/create")
    @OptionalLogConfig(methods = "分销商品")
    public ResponseResult<Boolean> modifyGoodsDealer(@RequestBody DealerGoodsCreateVo createVo) throws Exception {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(dealerGoodsBffService.modifyGoodsDealer(createVo.getGoodsIds(), createVo.getDealerIds(), createVo.getUnGoodsIds()));
        } catch (Exception e) {
            logger.error("[getDealerGoodsPage]商品分销列表异常 ", e);
            result.error("商品分销列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询存在经销商的省份信息")
    @GetMapping(value = "/province")
    public ResponseResult<List<DealerProvinceVo>> getDealerProvince() {
        ResponseResult<List<DealerProvinceVo>> result = new ResponseResult<>();
        try {
            result.success(dealerBffService.getDealerProvince());
        } catch (BizException e) {
            logger.error("[getDealerByProvinceId]查询存在经销商的省份信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerByProvinceId]查询存在经销商的省份信息异常", e);
            result.error("查询存在经销商的省份信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询经销商信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "provinceId", value = "省id", required = true, dataType = "Integer")})
    @GetMapping(value = "/province/{provinceId}")
    public ResponseResult<List<DealerVo>> getDealerByProvinceId(@PathVariable("provinceId") Integer provinceId) {
        ResponseResult<List<DealerVo>> result = new ResponseResult<>();
        try {
            result.success(dealerBffService.getDealer(provinceId));
        } catch (BizException e) {
            logger.error("[getDealerByProvinceId]根据省id(" + provinceId + ")查询经销商信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerByProvinceId]根据省id(" + provinceId + ")查询经销商信息异常", e);
            result.error("查询经销商信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询门店信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer")})
    @GetMapping(value = "/store/dealer/{dealerId}")
    public ResponseResult<List<StoreVo>> getStoreByDealerId(@PathVariable("dealerId") Integer dealerId) {
        ResponseResult<List<StoreVo>> result = new ResponseResult<>();
        try {
            result.success(storeBffService.getStoreByDealerId(dealerId));
        } catch (BizException e) {
            logger.error("[getStoreByDealerId]根据经销商id(" + dealerId + ")查询门店信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreByDealerId]根据经销商id(" + dealerId + ")查询门店信息异常", e);
            result.error("查询门店信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询营业中门店信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer")})
    @GetMapping(value = "/store/dealer/open/{dealerId}")
    public ResponseResult<List<StoreVo>> getOpenStoreByDealerId(@PathVariable("dealerId") Integer dealerId) {
        ResponseResult<List<StoreVo>> result = new ResponseResult<>();
        try {
            result.success(storeBffService.getOpenStoreByDealerId(dealerId));
        } catch (BizException e) {
            logger.error("[getStoreByDealerId]根据经销商id(" + dealerId + ")查询营业中门店信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreByDealerId]根据经销商id(" + dealerId + ")查询营业中门店信息异常", e);
            result.error("查询营业中门店信息异常");
        }
        return result;
    }


    @ApiIgnore
    @Override
    @ApiOperation(value = "创建门店围栏")
    @PostMapping(value = "/dealer/store/fence/create")
    @OptionalLogConfig(methods = "创建门店围栏")
    public ResponseResult<Boolean> createStoreFence() {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            result.success(storeBffService.createStoreFence());
        } catch (BizException e) {
            logger.error("[createStoreFence]创建门店围栏异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createStoreFence]创建门店围栏异常", e);
            result.error("创建门店围栏异常");
        }
        return result;
    }

    /**
     * 经销商列表
     *
     * @return
     */
    @Override
    @ApiOperation(value = "根据参数获取经销商列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shortName", value = "名称缩写", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "（不传或者0=查全部类型，1=只查营业中）", required = false, dataType = "String")
    })
    @GetMapping(value = "/dealer/all/listByParam")
    public ResponseResult<List<DealerVo>> getDealerListByParam(@RequestParam Map<String, Object> param) {
        ResponseResult<List<DealerVo>> result = new ResponseResult<>();
        try {
            return result.success(dealerBffService.getDealerListByParam(param));
        } catch (IllegalArgumentException e) {
            logger.error("[getDealerListByParam]获取经销商信息异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getDealerListByParam]获取经销商信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDealerListByParam]获取经销商信息异常 ", e);
            result.error("获取经销商信息异常");
        }
        return result;
    }

    /**
     * 获取已有围栏列表
     *
     * @param storeId
     * @return
     */
    @Override
    @ApiOperation(value = "查询已有围栏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店或中转库id", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/store/fence/binding/list")
    public ResponseResult<List<FenceVo>> getBindingFenceList(@RequestParam Integer storeId) {
        ResponseResult<List<FenceVo>> result = new ResponseResult<>();
        try {
            return result.success(fenceBffService.getBindingFenceList(storeId, FenceStoreStockTypeEnum.STORE.getValue()));
        } catch (IllegalArgumentException e) {
            logger.error("[getBindingFenceList]获取已有围栏异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getBindingFenceList]获取已有围栏异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getBindingFenceList]获取已有围栏异常 ", e);
            result.error("获取已有围栏异常");
        }
        return result;
    }

    /**
     * 获取可添加围栏列表
     *
     * @param storeId
     * @return
     */
    @Override
    @ApiOperation(value = "获取可添加围栏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店或中转库id", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/store/fence/unbinding/list")
    public ResponseResult<List<FenceVo>> getUnBindingFenceList(@RequestParam Integer storeId) {
        ResponseResult<List<FenceVo>> result = new ResponseResult<>();
        try {
            return result.success(fenceBffService.getUnBindingFenceList(storeId, FenceStoreStockTypeEnum.STORE.getValue()));
        } catch (IllegalArgumentException e) {
            logger.error("[getUnBindingFenceList]获取可添加围栏异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[getUnBindingFenceList]获取可添加围栏异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getUnBindingFenceList]获取可添加围栏异常 ", e);
            result.error("获取可添加围栏异常");
        }
        return result;
    }

    /**
     * 解除围栏
     *
     * @param fenceModifyVo
     * @return
     */
    @Override
    @ApiOperation(value = "解除围栏")
    @PostMapping(value = "/store/fence/delete")
    @OptionalLogConfig(methods = "解除围栏")
    public ResponseResult<Boolean> deleteFence(@RequestBody FenceModifyVo fenceModifyVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            int type = FenceStoreStockTypeEnum.STORE.getValue();
            fenceModifyVo.setType(type);
            return result.success(fenceBffService.deleteFence(fenceModifyVo));
        } catch (IllegalArgumentException e) {
            logger.error("[deleteFence]解除围栏异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[deleteFence]解除围栏异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[deleteFence]解除围栏异常 ", e);
            result.error("解除围栏异常");
        }
        return result;
    }

    /**
     * 添加围栏
     *
     * @param fenceCreateVo
     * @return
     */
    @Override
    @ApiOperation(value = "添加围栏")
    @PostMapping(value = "/store/fence/create")
    @OptionalLogConfig(methods = "添加围栏")
    public ResponseResult<Boolean> createFence(@RequestBody FenceCreateVo fenceCreateVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            int type = FenceStoreStockTypeEnum.STORE.getValue();
            fenceCreateVo.setType(type);
            return result.success(fenceBffService.createFence(fenceCreateVo));
        } catch (IllegalArgumentException e) {
            logger.error("[createFence]添加围栏异常", e);
            result.error(e.getMessage());
        } catch (BizException e) {
            logger.error("[createFence]添加围栏异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[createFence]添加围栏异常 ", e);
            result.error("添加围栏异常");
        }
        return result;
    }


}
